package com.ysg.widget.dialog;

import android.content.Context;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.ysg.R;
import com.ysg.base.BaseDialog;

/**
 * 隐私协议
 */
public class PrivacyDialog extends BaseDialog {
    public interface OnClickListener{
        void onAgreeClick(boolean isAgree);
        void onAgreementClick();
        void onPrivacyClick();
    }
    private OnClickListener listener;
    public void setOnClickListener(OnClickListener listener){
        this.listener = listener;
    }

    private TextView userPolicyInfo, userRead, userDisagree, userAgree;

    public PrivacyDialog(Context context) {
        super(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_privacy;
    }

    public void initView() {
        setCanceledOnTouchOutside(false);
        setCancelable(false);

        userPolicyInfo = findViewById(R.id.user_policy_info);
        userRead = findViewById(R.id.user_read);
        userDisagree = findViewById(R.id.user_disagree);
        userAgree = findViewById(R.id.user_agree);


        String start = getContext().getResources().getString(R.string.dialog_privacy_start);
        String middle = getContext().getResources().getString(R.string.dialog_privacy_middle);
        String end = getContext().getResources().getString(R.string.dialog_privacy_end);
        SpannableString policy = new SpannableString(getContext().getResources().getString(R.string.dialog_privacy_agreement));
        SpannableString privacy = new SpannableString(getContext().getResources().getString(R.string.dialog_privacy_privacy));

        ClickableSpan policySpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if (listener != null) {
                    listener.onAgreementClick();
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(mContext.getResources().getColor(R.color.pinkColor));
            }
        };
        ClickableSpan privacySpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if (listener != null) {
                    listener.onPrivacyClick();
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(mContext.getResources().getColor(R.color.pinkColor));
            }
        };

        policy.setSpan(policySpan, 0, policy.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        privacy.setSpan(privacySpan, 0, privacy.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append(start).append(policy).append(middle).append(privacy).append(end);
        userRead.setMovementMethod(LinkMovementMethod.getInstance());
        userRead.setText(ssb);
    }

    @Override
    public void initData() {
    }

    @Override
    public void initListener() {
        userDisagree.setOnClickListener((view)->{
            if (listener != null) {
                listener.onAgreeClick(false);
                cancel();
            }
        });
        userAgree.setOnClickListener((view)->{
            if (listener != null) {
                listener.onAgreeClick(true);
                cancel();
            }
        });
    }


}
